package com.niiwoo.civet.account.service.local.transform;

import com.niiwoo.civet.account.dao.entity.BankCard;
import com.niiwoo.civet.account.dao.mapper.BankCardMapperExt;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户数据转换临时Service
 */
@Service
@Slf4j
public class UserBankCardToDecodeService {

    @Autowired
    private BankCardMapperExt bankCardMapperExt;

    @Autowired
    private PrivacyMasks privacyMasks;


    public void toTransformTempUserExport(){
        log.info("银行卡代扣迁移开始转换数据");
        List<BankCard> bankCardList = bankCardMapperExt.selectBorrowerUserBankCard();
        log.info("银行卡代扣迁移开始转换数据，总条数:{}",bankCardList.size());
        int successCount = 0;
        int failedCount = 0;
        Date nowDate = new Date();

       for(BankCard bankCard : bankCardList){
            try{
                Map<String,Object> record = new HashMap<>();
                String bankCardNO = privacyMasks.decryptPrivacy(bankCard.getBankAccountNo());
                String idCardNO = privacyMasks.decryptPrivacy(bankCard.getIdCard());
                String mobile = privacyMasks.decryptPrivacy(bankCard.getMobileNo());
                record.put("id",bankCard.getId());
                record.put("bank_account_no",bankCardNO);
                record.put("id_card_no",idCardNO);
                record.put("real_name",bankCard.getRealName());
                record.put("mobile_no",mobile);
                record.put("user_id",bankCard.getUserId());
                record.put("bank_code",bankCard.getBankCode());
                record.put("create_time",nowDate);
                this.saveRow(record);
                successCount++;
            }catch(Exception e){
                failedCount ++;
                log.error("银行卡转换异常ID:{}",bankCard.getId(),e);
            }
        }
        log.info("银行卡代扣迁移结束转换数据，成功总条数:{},失败总条数:{}",successCount,failedCount);
    }

    public void saveRow(Map<String,Object> record){
        bankCardMapperExt.insertTempUserExport(record);
    }
}
